import Vuex from 'vuex';
import Vue from 'vue';
import createPersistedState from "vuex-persistedstate";
Vue.use(Vuex);

export default new Vuex.Store({
    plugins: [createPersistedState()],
state:{
    phone:'13800808080',
    token:'',
    cartDataList: []
},

mutations:{
        set_phone(state,payload){
            state.phone = payload;
        },
        set_token(state,payload){
            state.token = payload;
        },
        SET_CART_DATA(state, data) {
            state.cartDataList = data;
        },
        ADD_TO_CART(state, item) {
            state.cartDataList.push(item);
        }
    },
    actions: {
        async fetchCartDataList({ commit }) {
            try {
                const res = await request.get("/cart/list");
                console.log(res);
                commit('SET_CART_DATA', res.list);
            } catch (error) {
                console.error('获取购物车数据失败:', error);
                Toast.fail('获取购物车数据失败');
            }
        }
    },
    plugins: [createPersistedState()]
    
});



